package com.fingard.remoting.server;

import java.util.HashMap;

import com.fingard.FGBiz;
import com.fingard.io.FileHelper;
import com.fingard.xml.XmlTextReader;

public class TenantSet {
	
	public boolean enable;
	public String ownPublicKey;
	public String ownPrivateKey;
	private HashMap<String, TenantItem> mapTenant;
	
	public boolean hasTenant(String pTenant){
		if(mapTenant!=null &&mapTenant.containsKey(pTenant)){
			return true;
		}else{
			return false;
		}
	}
	
	public TenantSet(){
		enable = false;
	}
	
	public void loadFromXmlFile(String pXmlFilePath){
		if(!FileHelper.existFile(pXmlFilePath)){
			return;
		}
		try{
			String configText = FileHelper.readFileText(pXmlFilePath, "utf-8");
			XmlTextReader reader = new XmlTextReader(configText);
			if("true".equals(reader.getSimpleTextNotNull("enable").toLowerCase())){
				enable = true;
			}else{
				enable = false;
			}
			ownPublicKey = reader.getSimpleTextNotNull("ownPublicKey");
			ownPrivateKey = reader.getSimpleTextNotNull("ownPrivateKey");
			mapTenant = new HashMap<String, TenantItem>();
			while(reader.read("/config/tenant")){
				TenantItem eachItem = new TenantItem();
				eachItem.fromXElement(reader.readedXElement);
				mapTenant.put(eachItem.code, eachItem);
			}
		}catch(Exception e){
			enable = false;
			FGBiz.limitMsg.exception("TenantSet.loadFromXmlFile", e);
		}
	}
}
